<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns search-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName === '1'">收起筛选</p>
              <p v-if="activeName === ''">展开筛选</p>
            </div>
          </template>
          <div class="search-btn search-btns" @click.stop="queryPageList()">
            <i class="el-icon-circle-check"></i>
            <p>查询</p>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">养殖品种：</div>
            <el-select filterable clearable v-model="searchObj.varietyId" filterable clearable allow-create>
              <el-option v-for="item of FishVarietyData" :key="item.fishVarietyId + 'variety'" :label="item.varietyName"
                :value="item.fishVarietyId" />
            </el-select>
          </div>
        </el-collapse-item>
      </el-collapse>
    </template>
    <template v-slot:btnsBar>
      <ActionBar :add-active="addActive" :del-active="delActive" :page-no="163" @add="add" @del="del" />
    </template>
    <template v-slot:content>
      <el-table ref="tableData" v-loading="loading" highlight-current-row :data="tableData" stripe border
        height="calc(100% - 50px)" :row-key="row => {
          return row.varietyPictureId;
        }
          " @selection-change="handleSelectionChange" @select="select" @select-all="selectAll">
        <el-table-column type="selection" width="55" :reserve-selection="true" :selectable="selectable" />
        <el-table-column type="index" label="序号" width="50">
          <template slot-scope="scoped">{{ scoped.row.varietyPictureId ? scoped.$index + 1 : '' }}</template>
        </el-table-column>
        <el-table-column prop="varietyName" label="品种名称" />
        <el-table-column label="品种图片">
          <template v-if="scoped.row.pictureImg" slot-scope="scoped">
            <img style="width: 100px;height: 100px;" :src="scoped.row.pictureImg" alt="">
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" />
        <el-table-column label="操作" width="150">
          <template v-if="scoped.row.varietyPictureId" slot-scope="scoped">
            <div class="table-btns-box">
              <el-button type="primary" plain @click="edit(scoped.row)">编辑</el-button>
            <el-button type="danger" plain @click="delOne(scoped.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
        @changeCurrentPage="changeCurrentPage" />
      <AddEditDialog ref="addEditDialog" :now-chance-stau="nowChanceStau" :title-text="titleText"
        :edit-object="editObject" :fishVarietyData="FishVarietyData" :show.sync="showAddEditDialog"
        @closeDialog="closeDialog" @cancelDialog="cancel" />
    </template>
  </BaseMainSlot>
</template>

<script>
import DocumentApi from "@/api/document";
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new2.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";

import fishSpeciesImgApi from "@/api/traceability/basicsInfo/fishSpeciesImg";
import FishVarietyApi from "@/api/basic-info-manage/fishVariety";
import FishPondApi from "@/api/basic-info-manage/fishPond";
import { blobToBase64 } from "@/utils/convert";
export default {
  name: "BatchCreate",
  components: { ActionBar, Pagination, BaseMainSlot, AddEditDialog },
  mixins: [mymixin],
  data () {
    return {

      closePondDateTimeList: [],
      mnecode: "", //批次编号
      productionBatchCode: "", //码单号
      fishPondId: "", //养殖鱼塘ID
      fishVarietyId: "", //养殖品种ID
      buildDateTime: "", //批次创建时间
      checkedIdList: [],
      fishPondList: [], //全部鱼塘
      FishVarietyData: [], //全部品种
      showAddEditDialog: false,
      routerParams: {},
      searchObj: { fishVarietyId: '' }
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created () {
    this.routerParams = this.$route.params ? this.$route.params : {}
    console.log(this.routerParams, '路由参数')
  },
  mounted () {
    this.queryFishVariety();
    this.queryFishPond();
    this.queryPageList();
  },
  methods: {
    async fileDownload (fileId) {
      let res = fileId && await DocumentApi.fileDownload({ fileId })
      console.log(res, '---')
      let img = await blobToBase64(res.data)
      return img
    },
    queryPageList () {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          status: 'CREATE',
          ...this.searchObj
        }
      };
      fishSpeciesImgApi.queryPageByCondition(params).then(res => {
        if (res.success === true) {
          res.data.content.forEach(async (item, index) => {
            item['pictureImg'] = this.$baseurl.webServiceUrl + 'base/files/preview?fileId=' + item.pictureId
          })
          this.totalSize = res.data.totalElements;
          this.tableData = res.data.content;
          this.addblankmet();
          this.resetActionBar();
        }
        this.routerParams = {}
      });
    },
    //查询可选鱼类品种
    queryFishVariety () {
      FishVarietyApi.queryByCondition().then(res => {
        this.FishVarietyData = res.data;
      });
    },
    //查询可选鱼塘
    queryFishPond () {
      FishPondApi.queryByCondition().then(res => {
        this.fishPondList = res.data;
      });
    },
    changeCurrentPage (currentPage) {
      this.currentPage = currentPage;
      this.queryPageList();
    },
    selectAll (list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.varietyPictureId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select (list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.varietyPictureId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    handleSelectionChange (list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map(item => {
        if (item.varietyPictureId) {
          return item.varietyPictureId;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        // this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 表格删除
    delmessage () {
      fishSpeciesImgApi.delListById({ idList: this.checkedIdList }).then(res => {
        if (res.success) {
          this.$message.success("删除成功");
          this.queryPageList();
          this.resetActionBar();
        }
      });
    },
    // 单个删除
    delOne (row) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        
      })
        .then(() => {
          fishSpeciesImgApi.delById({ id: row.varietyPictureId }).then(res => {
            if (res.success === true) {
              this.$message.success("删除成功!");
              this.queryPageList();
              this.resetActionBar();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 添加
    add () {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit (row) {
      if (row.varietyPictureId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    },
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-range-separator {
  color: #333333;
}

.select-box-content-name {
  min-width: 80px;
}

.select-box-content {
  width: none !important;
}
</style>
